package com.frxuexi.park2301.my

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.DatePicker
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.EmpBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.utils.GlideUtil
import com.frxuexi.park2301.R
import com.frxuexi.park2301.databinding.ActivityMyInfoBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@Route(path = "/app/MyInfoActivity")
@AndroidEntryPoint
class MyInfoActivity : BaseActivity<ActivityMyInfoBinding, MyViewModel>() {
    var path = ""
    lateinit var emp:EmpBean
    override fun initData() {
        vm.addEmpData.observe(this) {
            if (it > 0) {
                ToastUtils.showLong("新增成功")
                Log.i("lr", "新增成功：${it}")
            }
        }

        vm.updateEmpData.observe(this) {
            if (it > 0) {
                ToastUtils.showLong("修改成功")
                Log.i("lr", "修改成功：${it}")
            }
        }

        vm.deleteEmpData.observe(this) {
            if (it > 0) {
                ToastUtils.showLong("删除成功")
                Log.i("lr", "删除成功：${it}")
            }
        }

        vm.getAllEmpData.observe(this){
            Log.i("lr",it.toString())
        }

        vm.getLastEmpData.observe(this) {
            emp=it
            Log.i("lr", it.toString())
            binding.empaddNo.setText(it.id.toString())
            binding.empaddName.setText(it.name)
            binding.empaddPhone.setText(it.phone)
            binding.empaddCarNo.setText(it.carNo)
            binding.empaddDate.setText(it.time)
            binding.empaddDepart.setText(it.depart)
            if(it.sex=="man"){
                binding.empaddRg.check(R.id.pardadd_man)
            }else{
                binding.empaddRg.check(R.id.pardadd_women)
            }
            GlideUtil.getCircleImg(it.img,binding.empaddIv)
        }


    }

    override fun initView() {
        vm.getLastEmp()
        binding.btnAdd.setOnClickListener {
            val name = binding.empaddName.text.toString()
            val phone = binding.empaddPhone.text.toString()
            val carNo = binding.empaddCarNo.text.toString()
            val date = binding.empaddDate.text.toString()
            val depart = binding.empaddDepart.text.toString()
            var sex = "男"
            if (binding.empaddRg.checkedRadioButtonId == R.id.pardadd_man) {
                sex = "男"
            } else {
                sex = "女"
            }
            val empBean = EmpBean(null, name, phone, sex, carNo, date, depart, path)
            vm.addEmp(empBean)
        }

        binding.empaddIvdate.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                object : OnDateSetListener {
                    override fun onDateSet(
                        view: DatePicker?,
                        year: Int,
                        month: Int,
                        dayOfMonth: Int
                    ) {
                        val m = if (month + 1 < 10) "0${month + 1}" else "${month + 1}"
                        val day = if (dayOfMonth < 10) "0${dayOfMonth}" else "${dayOfMonth}"
                        val date = "${year}-${m}-${day}"
                        binding.empaddDate.setText(date)
                    }
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.empaddChange.setOnClickListener {
            PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.btnUpdate.setOnClickListener {
            val no = binding.empaddNo.text.toString().toLong()
            val name = binding.empaddName.text.toString()
            val phone = binding.empaddPhone.text.toString()
            val carNo = binding.empaddCarNo.text.toString()
            val date = binding.empaddDate.text.toString()
            val depart = binding.empaddDepart.text.toString()
            var sex = "男"
            if (binding.empaddRg.checkedRadioButtonId == R.id.pardadd_man) {
                sex = "男"
            } else {
                sex = "女"
            }
            val empBean = EmpBean(no, name, phone, sex, carNo, date, depart, path)
            vm.updateEmp(empBean)
        }

        binding.btnDelete.setOnClickListener {
            vm.deleteEmp(emp)
        }

        binding.btnSelect.setOnClickListener {
            vm.getAllEmp()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            path = bean?.path!!
            GlideUtil.getCircleImg(path, binding.empaddIv)
        }
    }

}